//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.sunmi.sbs.commons.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Random;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class LoginUtils {
    private static String env;
    private static String currentPath;
    private static String region;
    private static JSONObject apiConfigData;
    private static JSONObject configData;
    private static LoginUtilsHandler loginUtilsHandler;

    public LoginUtils(String env, String currentPath, String region) throws Exception {
        try {
            loginUtilsHandler = new LoginUtilsHandler(env, currentPath);
//            LoginUtilsHandler var10000 = loginUtilsHandler;
            configData = LoginUtilsHandler.envConfigs;
            LoginUtils.env = env;
            LoginUtils.region = region;
            LoginUtils.currentPath = currentPath;
            apiConfigData = LoginUtilsHandler.apiConfigData;
        } catch (Exception var5) {
            throw new Exception("LoginUtils 实例化失败 ===> " + var5);
        }
    }

    private static JSONObject getHeaders(String arguments, String appId, String appKey) {
        JSONObject headers = new JSONObject();
        if (!arguments.isEmpty() && !appId.isEmpty() && !appKey.isEmpty()) {
            Random randomNum = new Random();
            String nonce = String.valueOf(randomNum.nextInt(900000) + 100000);
            String timeStamp = String.valueOf(System.currentTimeMillis());
            String connectStrSign = arguments + appId + timeStamp + nonce;
            String sign = HMACUtils.hmac256(connectStrSign, appKey);
            headers.put("MAX-TIMESTAMP", timeStamp);
            headers.put("MAX-NONCE", nonce);
            headers.put("MAX-APP-ID", appId);
            headers.put("MAX-SIGN-TYPE", "1");
            headers.put("MAX-SIGN", sign);
            return headers;
        } else {
            System.out.println("参数不可为空");
            return null;
        }
    }

    public static JSONObject maxLogin(String userName, String passWord, String appId, String appKey, String orgId) throws Exception {
        if (!userName.isEmpty() && !passWord.isEmpty()) {
            String maxLoginHost = configData.getJSONObject("hostInfo").getJSONObject(env).getJSONObject("max").getString("host");
            System.out.println("当前host : " + maxLoginHost);
            JSONObject desInfo = configData.getJSONObject("desInfo").getJSONObject(env);
            String desIv = desInfo.getString("Iv");
            String desKey = desInfo.getString("Key");
            String passWord2Encode = DesUtils.encode(desKey, passWord, desIv);
            System.out.println("password: " + passWord2Encode);
            String maxLoginPath = configData.getJSONObject("loginPath").getString("loginByLoginName");
            JSONObject payload = new JSONObject();
            JSONObject request = new JSONObject();
            JSONObject context = new JSONObject();
            request.put("userName", userName);
            request.put("password", passWord2Encode);
            request.put("strategy", 1);
            context.put("appId", appId);
            context.put("orgId", orgId);
            payload.put("request", request);
            payload.put("context", context);
            JSONObject maxLoginHeaders = getHeaders(String.valueOf(payload), appId, appKey);
            System.out.println("payload : " + String.valueOf(payload));
            System.out.println("maxLoginHeaders : " + String.valueOf(maxLoginHeaders));

            try {
                JSONObject maxLoginResp = HttpClientUtils.httpclient(maxLoginHost, maxLoginPath, maxLoginHeaders, payload);
                if ("1".equals(maxLoginResp.getString("code"))) {
                    return maxLoginResp;
                } else {
                    throw new Exception("maxLogin登录失败。 maxLoginResp ： " + maxLoginResp);
                }
            } catch (Exception var16) {
                throw new Exception("maxLogin Fail. Exception : " + var16);
            }
        } else {
            System.out.println("参数不可为空");
            return null;
        }
    }

    public static String getMaxLoginRefreshToken(String userName, String passWord, String appId, String appKey, String orgId) throws Exception {
        try {
            JSONObject maxLoginResp = maxLogin(userName, passWord, appId, appKey, orgId);
            JSONObject maxLoginRespData = maxLoginResp.getJSONObject("data");
            String refreshToken = maxLoginRespData.getString("refreshToken");
            return refreshToken;
        } catch (Exception var8) {
            throw new Exception(var8);
        }
    }

    public static JSONObject idCenterLogin(String userName, String passWord) throws Exception {
        String idCenterLoginHost = configData.getJSONObject("hostInfo").getJSONObject(env).getJSONObject("idCenter").getString("host");
        String idCenterLoginPath = configData.getJSONObject("loginPath").getString("login");
        JSONObject idCenterAppKeyMap = new JSONObject();
        idCenterAppKeyMap.put("dev", "b4600f3d134891b12c5c9003f169cdc3");
        idCenterAppKeyMap.put("test", "c2d3c48ee4d33f4e751ae5c37cf5739c");
        idCenterAppKeyMap.put("pre", "3fdd5f1937ac7038ea1f0a7ec632f93b");
        idCenterAppKeyMap.put("onl", "15b4a32ce0f8fefc19a862f0d261542d");
        JSONObject desInfo = configData.getJSONObject("desInfo").getJSONObject(env);
        String desIv = desInfo.getString("Iv");
        String desKey = desInfo.getString("Key");
        String passWord2Encode = DesUtils.encode(desKey, passWord, desIv);
        CloseableHttpClient httpclient = HttpClients.createDefault();
        JSONObject idCenterLoginPayload = new JSONObject();
        idCenterLoginPayload.put("user_name", userName);
        idCenterLoginPayload.put("password", passWord2Encode);
        idCenterLoginPayload.put("verify_code_sessionId", "");
        idCenterLoginPayload.put("verify_code_sig", "");
        idCenterLoginPayload.put("verify_code_token", "");
        Random randomNum = new Random();
        String nonce = String.valueOf(randomNum.nextInt(900000) + 100000);
        String timeStamp = String.valueOf(System.currentTimeMillis());
        String appKey = idCenterAppKeyMap.getString(env);
        String sign = HMACUtils.hmac256(idCenterLoginPayload.toString() + "" + timeStamp + nonce, appKey);
        JSONObject idCenterLoginHeaders = new JSONObject();
        idCenterLoginHeaders.put("x-token", "");
        idCenterLoginHeaders.put("x-timestamp", timeStamp);
        idCenterLoginHeaders.put("x-nonce", nonce);
        idCenterLoginHeaders.put("x-sign", sign);
        idCenterLoginHeaders.put("x-client-name", "account-center");
        idCenterLoginHeaders.put("x-client-version", "1.0.1");
        idCenterLoginHeaders.put("x-language", "zh-CN");
        idCenterLoginHeaders.put("x-timezone", "Asia/Shanghai");
        idCenterLoginHeaders.put("X-AutoTest", "sunmi388");

        try {
            JSONObject idCenterLoginResp = HttpClientUtils.httpclient(idCenterLoginHost, idCenterLoginPath, idCenterLoginHeaders, idCenterLoginPayload);
            if ("1".equals(idCenterLoginResp.getString("code"))) {
                JSONObject resp = new JSONObject();
                resp.put("idCenterLoginResp", idCenterLoginResp);
                resp.put("sunmi-nonce", nonce);
                resp.put("sunmi-timestamp", timeStamp);
                resp.put("sunmi-sign", sign);
                return resp;
            } else {
                throw new Exception("idCenterLogin is Fail. code != 1. idCenterLoginResp : " + String.valueOf(idCenterLoginResp));
            }
        } catch (Exception var19) {
            throw new Exception("idCenterLogin is Fail. Exception : " + var19);
        }
    }

    public static String getIdCenterLoginToken(String userName, String passWord) throws Exception {
        try {
            JSONObject idCenterLoginResp = idCenterLogin(userName, passWord);
            idCenterLoginResp = idCenterLoginResp.getJSONObject("idCenterLoginResp");
            String idCenterLoginToken = idCenterLoginResp.getString("data");
            return idCenterLoginToken;
        } catch (Exception var4) {
            throw new Exception(var4);
        }
    }

    public static JSONObject persistencToken(String userName, String passWord, String appId, String appKey, String orgId) throws Exception {
        String refreshToken = getMaxLoginRefreshToken(userName, passWord, appId, appKey, orgId);
        String idCenterToken = getIdCenterLoginToken(userName, passWord);
        String exchangeUserTokenPath = configData.getJSONObject("loginPath").getString("exchangeUserToken");
        String maxiotHost = configData.getJSONObject("hostInfo").getJSONObject(env).getJSONObject("maxiot").getString("host");
        JSONObject exchangeUserTokenHeaders = new JSONObject();
        exchangeUserTokenHeaders.put("X-TOKEN", refreshToken);
        JSONObject exchangeUserTokenPayload = new JSONObject();
        JSONObject exchangeUserTokenResp = HttpClientUtils.httpclient(maxiotHost, exchangeUserTokenPath, exchangeUserTokenHeaders, exchangeUserTokenPayload);
        String exchangeUserTokenRespCode = exchangeUserTokenResp.getString("code");
        if ("1".equals(exchangeUserTokenRespCode)) {
            String exchangeUserToken = exchangeUserTokenResp.getJSONObject("data").getString("token");
            if (exchangeUserToken.equals(idCenterToken)) {
                JSONObject resp = new JSONObject();
                resp.put("refreshToken", refreshToken);
                resp.put("idCenterToken", exchangeUserToken);
                return resp;
            } else {
                throw new Exception("交换Token 与 idCenterToken 不一致");
            }
        } else {
            throw new Exception("JWT 交换 idCenterToken 失败。");
        }
    }

    public static String getAccessToken(String refreshToken, String appId, String appKey) throws Exception {
        String maxHost = configData.getJSONObject("hostInfo").getJSONObject(env).getJSONObject("max").getString("host");
        String getAccessTokenPath = configData.getJSONObject("loginPath").getString("getAccessToken");
        JSONObject payload = new JSONObject();
        JSONObject request = new JSONObject();
        JSONObject context = new JSONObject();
        request.put("refreshToken", refreshToken);
        context.put("appId", appId);
        payload.put("request", request);
        payload.put("context", context);

        JSONObject getAccessTokenHeaders = getHeaders(payload.toJSONString(), appId, appKey);
        try {
            JSONObject getAccessTokenResp = HttpClientUtils.httpclient(maxHost, getAccessTokenPath, getAccessTokenHeaders, payload);
            if ("1".equals(getAccessTokenResp.getString("code"))) {
                String theAccessToken = getAccessTokenResp.getJSONObject("data").getString("accessToken");
                return theAccessToken;
            } else {
                throw new Exception("maxLogin登录失败。 maxLoginResp ： " + getAccessTokenResp.toJSONString());
            }
        } catch (Exception var16) {
            throw new Exception("maxLogin Fail. Exception : " + var16);
        }
    }
}
